//
//  SalesViewController.m
//  JinChengYun
//
//  Created by 金橙云 on 2017/3/9.
//  Copyright © 2017年 金橙云. All rights reserved.
//


#import "SalesViewController.h"//销售动态


#import "TodoHttpManager.h"
#import "OneCustomView.h"


#import "BiaoGeCell.h"
#import "ZheXianFirstCell.h"
#import "ZhuTuFIrstCell.h"

#import "MJRefresh.h"
@interface SalesViewController ()<OneCustomViewDelagate,UITableViewDelegate,UITableViewDataSource>

{
    NSArray *_hengyiArr;

}

@property(nonatomic,strong)UIScrollView *scrollerView;
@property(nonatomic,strong)OneCustomView *oneCustomV;

@property(nonatomic,strong)NSMutableDictionary *bigDic;
@property(nonatomic,strong)NSMutableArray *biaoArr;

@property(nonatomic,strong)UITableView *tabView;


@property (nonatomic, strong) NSMutableArray         *zuoyishuArr;//左一
@property (nonatomic, strong) NSMutableArray         *nozuiyiArr;//无左一
@property (nonatomic, strong) NSArray         *xiaoshouAnalayseArr;//销售动态 日周月季度年 默认===日
@property (nonatomic, strong) NSArray         *zhuMoneyAnalayseArr;//业绩动态 日周月季度年 默认===日



@end

@implementation SalesViewController

-(void)viewWillAppear:(BOOL)animated
{
    _manager = [TodoHttpManager sharedManager];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didRefreshManagerFirstSales:) name:TDManagerFirstSales object:nil];
   
}

-(void)didRefreshManagerFirstSales:(NSNotification *)noy
{
    if (noy.object != 0)
    {
        [_tabView reloadData];
    }
}
-(void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:TDManagerFirstSales object:nil];
}



- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    _hengyiArr = @[@"类型",@"今日",@"本月",@"累计",@"去化"];


    _zuoyishuArr = [NSMutableArray arrayWithCapacity:0];
    _nozuiyiArr = [NSMutableArray arrayWithCapacity:0];

    _bigDic = [NSMutableDictionary dictionaryWithCapacity:0];
    _biaoArr = [NSMutableArray arrayWithCapacity:0];
    
    _tabView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, B_WIDTH, B_HEIGHT-LL_StatusBarAndNavigationBarHeight-45-LL_TabbarHeight)];
    _tabView.delegate = self;
    _tabView.dataSource = self;
    _tabView.backgroundColor = BgColor;
    _tabView.tableHeaderView = self.oneCustomV;
    _tabView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [_tabView registerClass:[BiaoGeCell class] forCellReuseIdentifier:@"ID"];
    [_tabView registerClass:[ZheXianFirstCell class] forCellReuseIdentifier:@"IDA"];
    [_tabView registerClass:[ZhuTuFIrstCell class] forCellReuseIdentifier:@"IDB"];
    [self.view addSubview:_tabView];
    
    _tabView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(xiaoShouRefersh)];

   
}

-(void)xiaoShouRefersh
{
     [_manager userManagerFirstSalesDynamic];
    [self.tabView.mj_header endRefreshing];
}

-(OneCustomView *)oneCustomV
{
    NSString *IDA = [[NSUserDefaults standardUserDefaults] objectForKey:@"STATE"];
    if (_oneCustomV )
    {
        return _oneCustomV;
    }
    if ([IDA isEqualToString:@"2"])
    {
        _oneCustomV = [[OneCustomView alloc] initWithFrame:CGRectMake(0, 0, B_WIDTH, 120)];
    }
    else
    {
        _oneCustomV = [[OneCustomView alloc] initWithFrame:CGRectMake(0, 0, B_WIDTH, 34)];
    }
   
    _oneCustomV.delegate = self;
    _oneCustomV.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_oneCustomV];
    return _oneCustomV;
}
#pragma mark-- 自定义头部的代理
-(void)setDelegate:(NSMutableDictionary *)Dict
{
    _bigDic = Dict;
    
    // NSLog(@"Dict=====%@",dic);
    //表格数据
    _biaoArr = [_bigDic objectForKey:@"sale"];
    if (_nozuiyiArr.count >0)
    {
        [_nozuiyiArr removeAllObjects];
    }
    if (_zuoyishuArr.count>0)
    {
        [_nozuiyiArr removeAllObjects];
    }
    
    for (int i=0; i<_biaoArr.count; i++) {
        NSDictionary *tmpDic = _biaoArr[i];
        [_zuoyishuArr addObject:tmpDic[@"name"]];
        NSMutableArray *arr = [NSMutableArray arrayWithObjects:tmpDic[@"today_sale"],tmpDic[@"month_sale"],tmpDic[@"all_sale"],tmpDic[@"bl2"], nil];
        
        [_nozuiyiArr addObject:arr];
    }
    //  NSLog(@"_nozuiyiArr===%@",_nozuiyiArr);
    
    
    [_tabView reloadData];
    
    //第一个折线图 日默认**********************************
    /**
     日
     */
    NSDictionary *firstDic = [_bigDic objectForKey:@"day_sale_analyse"];
    NSArray *dataArr = firstDic[@"data"];
    NSMutableArray *dayCountArr = [NSMutableArray new];
    NSMutableArray *dayNameArr = [NSMutableArray new];
    for (int i=0; i<dataArr.count; i++) {
        NSDictionary *tmpDic = dataArr[i];
        [dayCountArr addObject:tmpDic[@"count"]];//纵坐标
        [dayNameArr addObject:tmpDic[@"name"]];//横坐标
        
    }
    /**
     周
     */
    NSDictionary *zhouSalesDic = [_bigDic objectForKey:@"zhou_sale_analyse"];
    NSArray *zhouDataArr = zhouSalesDic[@"data"];
    NSMutableArray *zhouCountArr = [NSMutableArray new];
    NSMutableArray *zhouNameArr = [NSMutableArray new];
    for (int i=0; i<zhouDataArr.count; i++) {
        NSDictionary *tmpDic = zhouDataArr[i];
        [zhouCountArr addObject:tmpDic[@"count"]];//纵坐标
        [zhouNameArr addObject:tmpDic[@"name"]];//横坐标
    }
    /**
     月
     */
    NSDictionary *monthSalesDic = [_bigDic objectForKey:@"month_sale_analyse"];
    NSArray *monthDataArr = monthSalesDic[@"data"];
    NSMutableArray *monthCountArr = [NSMutableArray new];
    NSMutableArray *monthNameArr = [NSMutableArray new];
    for (int i=0; i<monthDataArr.count; i++) {
        NSDictionary *tmpDic = monthDataArr[i];
        [monthCountArr addObject:tmpDic[@"count"]];//纵坐标
        [monthNameArr addObject:tmpDic[@"name"]];//横坐标
        
    }
    /**
     季度
     */
    NSDictionary *jiduSalesDic = [_bigDic objectForKey:@"jidu_sale_analyse"];
    NSArray *jiduDataArr = jiduSalesDic[@"data"];
    NSMutableArray *jiduCountArr = [NSMutableArray new];
    NSMutableArray *jiduNameArr = [NSMutableArray new];
    for (int i=0; i<jiduDataArr.count; i++) {
        NSDictionary *tmpDic = jiduDataArr[i];
        [jiduCountArr addObject:tmpDic[@"count"]];//纵坐标
        [jiduNameArr addObject:tmpDic[@"name"]];//横坐标
        
    }
    /**
     季度
     */
    NSDictionary *nianSalesDic = [_bigDic objectForKey:@"nian_sale_analyse"];
    NSArray *nianDataArr = nianSalesDic[@"data"];
    NSMutableArray *nianCountArr = [NSMutableArray new];
    NSMutableArray *nianNameArr = [NSMutableArray new];
    for (int i=0; i<nianDataArr.count; i++) {
        NSDictionary *tmpDic = nianDataArr[i];
        [nianCountArr addObject:tmpDic[@"count"]];//纵坐标
        [nianNameArr addObject:tmpDic[@"name"]];//横坐标
        
    }
    //NSLog(@"-------999999---%@-------%@",nianCountArr,nianNameArr);
    
    _xiaoshouAnalayseArr = @[@[dayCountArr,dayNameArr],@[zhouCountArr,zhouNameArr],@[monthCountArr,monthNameArr],@[jiduCountArr,jiduNameArr],@[nianCountArr,nianNameArr]];
    
    
    // 柱状图 日默认**********************************
    //    /**
    //     日
    //     */
    //    NSDictionary *zhuFirstDic = [_bigDic objectForKey:@"day_money_analyse"];
    //    NSArray *zhudataArr = zhuFirstDic[@"data"];
    //    NSMutableArray *zhudayCountArr = [NSMutableArray new];
    //    NSMutableArray *zhudayNameArr = [NSMutableArray new];
    //    NSString *zhuDayMax = [NSString stringWithFormat:@"%@",zhuFirstDic[@"max_data"]];
    //    for (int i=0; i<zhudataArr.count; i++) {
    //        NSDictionary *tmpDic = zhudataArr[i];
    //        [zhudayCountArr addObject:tmpDic[@"count"]];//纵坐标
    //        [zhudayNameArr addObject:tmpDic[@"name"]];//横坐标
    //    }
    //    /**
    //     周
    //     */
    //    NSDictionary *zhuzhouSalesDic = [_bigDic objectForKey:@"zhou_money_analyse"];
    //    NSArray *zhuzhouDataArr = zhuzhouSalesDic[@"data"];
    //    NSMutableArray *zhuzhouCountArr = [NSMutableArray new];
    //    NSMutableArray *zhuzhouNameArr = [NSMutableArray new];
    //    NSString *zhuZhouMax = [NSString stringWithFormat:@"%@",zhuFirstDic[@"max_data"]];
    //
    //    for (int i=0; i<zhuzhouDataArr.count; i++) {
    //        NSDictionary *tmpDic = zhuzhouDataArr[i];
    //        [zhuzhouCountArr addObject:tmpDic[@"count"]];//纵坐标
    //        [zhuzhouNameArr addObject:tmpDic[@"name"]];//横坐标
    //    }
    /**
     月
     */
    NSDictionary *zhumonthSalesDic = [_bigDic objectForKey:@"month_money_analyse"];
    NSArray *zhumonthDataArr = zhumonthSalesDic[@"data"];
    NSMutableArray *zhumonthCountArr = [NSMutableArray new];
    NSMutableArray *zhumonthNameArr = [NSMutableArray new];
    NSString *zhuMonthMax = [NSString stringWithFormat:@"%@",zhumonthSalesDic[@"max_data"]];
    for (int i=0; i<zhumonthDataArr.count; i++) {
        NSDictionary *tmpDic = zhumonthDataArr[i];
        [zhumonthCountArr addObject:tmpDic[@"count"]];//纵坐标
        [zhumonthNameArr addObject:tmpDic[@"name"]];//横坐标
        
    }
    /**
     季度
     */
    NSDictionary *zhujiduSalesDic = [_bigDic objectForKey:@"jidu_money_analyse"];
    NSArray *zhujiduDataArr = zhujiduSalesDic[@"data"];
    NSMutableArray *zhujiduCountArr = [NSMutableArray new];
    NSMutableArray *zhujiduNameArr = [NSMutableArray new];
    NSString *zhujiDuMax = [NSString stringWithFormat:@"%@",zhujiduSalesDic[@"max_data"]];
    for (int i=0; i<zhujiduDataArr.count; i++) {
        NSDictionary *tmpDic = zhujiduDataArr[i];
        [zhujiduCountArr addObject:tmpDic[@"count"]];//纵坐标
        [zhujiduNameArr addObject:tmpDic[@"name"]];//横坐标
        
    }
    /**
     年
     */
    NSDictionary *zhunianSalesDic = [_bigDic objectForKey:@"nian_money_analyse"];
    NSArray *zhunianDataArr = zhunianSalesDic[@"data"];
    NSMutableArray *zhunianCountArr = [NSMutableArray new];
    NSMutableArray *zhunianNameArr = [NSMutableArray new];
    NSString *zhuNianMax = [NSString stringWithFormat:@"%@",zhunianSalesDic[@"max_data"]];
    for (int i=0; i<zhunianDataArr.count; i++) {
        NSDictionary *tmpDic = zhunianDataArr[i];
        [zhunianCountArr addObject:tmpDic[@"count"]];//纵坐标
        [zhunianNameArr addObject:tmpDic[@"name"]];//横坐标
        
    }
    
    _xiaoshouAnalayseArr = @[@[dayCountArr,dayNameArr],@[zhouCountArr,zhouNameArr],@[monthCountArr,monthNameArr],@[jiduCountArr,jiduNameArr],@[nianCountArr,nianNameArr]];
    
    //_zhuMoneyAnalayseArr = @[@[zhudayCountArr,zhudayNameArr,zhuDayMax],@[zhuzhouCountArr,zhuzhouNameArr,zhuZhouMax],@[zhumonthCountArr,zhumonthNameArr,zhuMonthMax],@[zhujiduCountArr,zhujiduNameArr,zhujiDuMax],@[zhunianCountArr,zhunianNameArr,zhuNianMax]];
    
    _zhuMoneyAnalayseArr = @[@[zhumonthCountArr,zhumonthNameArr,zhuMonthMax],@[zhujiduCountArr,zhujiduNameArr,zhujiDuMax],@[zhunianCountArr,zhunianNameArr,zhuNianMax]];
    
   // [_tabView reloadData];
 
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 3;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   
    if (indexPath.row == 0)
    {
        NSString *ID = @"Cell";
        BiaoGeCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
        if (cell == nil)
        {
            cell = [[BiaoGeCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
        }
       // [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.userInteractionEnabled = NO;
        cell.backgroundColor = [UIColor redColor];
        if (_zuoyishuArr.count!=0&&_nozuiyiArr.count!=0&&_hengyiArr.count!=0) {
            cell.dataArr = @[_zuoyishuArr,_nozuiyiArr,_hengyiArr];
        }
        [cell.chatView reloadData];
        return cell;
    }
    if (indexPath.row == 1)
    {
        NSString *IDA = @"CellA";
        ZheXianFirstCell *cell = [tableView dequeueReusableCellWithIdentifier:IDA];
        if (cell == nil)
        {
            cell = [[ZheXianFirstCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:IDA];
        }
        
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        if (_xiaoshouAnalayseArr.count!=0) {
            cell.dataArr = [NSArray arrayWithArray:_xiaoshouAnalayseArr];
            cell.segmented.selectedSegmentIndex = 0;//默认第一个 日
            [cell.lineChart strokePath];

        }
        
        return cell;
    }
   else
    {
           NSString *IDB = @"CellB";
        ZhuTuFIrstCell *cell = [tableView dequeueReusableCellWithIdentifier:IDB];
        if (cell == nil)
        {
            cell = [[ZhuTuFIrstCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:IDB];
        }
        
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        if (_zhuMoneyAnalayseArr.count!=0) {
            cell.dataArr = _zhuMoneyAnalayseArr;
            cell.segMentIndex = 0;
            [cell.barChart strokePath];
        }
        
        return cell;
    }
  
 
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (indexPath.row == 0)
    {
        NSString *IDA = [[NSUserDefaults standardUserDefaults] objectForKey:@"STATE"];
        if ([IDA isEqualToString:@"2"])
        {
             return 330;
        }
      else
        {
             return 330;
        }
       
    }
    else if (indexPath.row == 1)
    {
        return 300;
    }
    else
    {
        return 320;
    }
}
//-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
//{
//    return 1;
//}








- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
